#include "utils.h"
void setUniforn1i(unsigned int program, char const *name, int value) {
    glUseProgram(program);
    glUniform1i(glGetUniformLocation(program, name), value);    
}

void setUniforn1f(unsigned int program, char const *name, float value1) {
    glUseProgram(program);
    glUniform1f(glGetUniformLocation(program, name), value1);    
}

void setUniforn3f(unsigned int program, char const *name, float value1, float value2, float value3) {
    glUseProgram(program);
    glUniform3f(glGetUniformLocation(program, name), value1, value2, value3);    
}

void setUniforn3vec(unsigned int program, char const *name, glm::vec3 value) {
    glUseProgram(program);
    glUniform3f(glGetUniformLocation(program, name), value.x, value.y, value.z);   
}

void setUniforn4mat(unsigned int program, char const *name, glm::mat4 value) {
    glUseProgram(program);
    glUniformMatrix4fv(glGetUniformLocation(program, name), 1, GL_FALSE, glm::value_ptr(value));   
}


void loadimg(unsigned int *texture, char const *path) {
    glGenTextures(1, texture);
    glBindTexture(GL_TEXTURE_2D, *texture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);   
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    // 加载并生成纹理
    stbi_set_flip_vertically_on_load(true);
    int width, height, nrChannels;
    unsigned char *data = stbi_load(path, &width, &height, &nrChannels, 0);
    if (data)
    {
        int format = GL_RGB;
        if (nrChannels == 3) {
            format = GL_RGB;
        } else if (nrChannels == 4) {
            format = GL_RGBA;
        }
        // printf("nrChannels: %d\n", nrChannels);
        glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
        glGenerateMipmap(GL_TEXTURE_2D);
    }
    else
    {
        printf("Failed to load texture\n");
    }    
    stbi_image_free(data);
}

void dir(char const *path, char *pathName) {
    int pIndex = 0;
    for (int i = 0, l = strlen(path); i < l; ++i) {
        if (path[i] == '/' || path[i] == '\\') {
            pIndex = i;
        }
    }
    strncpy(pathName, path, pIndex + 1);
}

int charNum(char const *str, char characher) {
    int result = 0;
    char *p = (char *)str;
    while (p[0] != '\0') {
        if (p[0] == characher) {
            result++;
        }
        p++;
    }
    return result;
}

string loadFile(string path){
	string dest;
	ifstream file;
	file.exceptions(ifstream::failbit | ifstream::badbit);
	try{
		file.open(path.c_str());
		stringstream stream_;
		stream_<<file.rdbuf();
		file.close();
		dest = stream_.str();
	}catch(ifstream::failure e){
		printf("Faile to load file: %s", path.c_str());
		dest = "";
	}
	return dest;
}

map<string, vector<string> > getPropertices(string src){
	map<string, vector<string> > propertices;
	vector<string> bufs = slipt(loadFile(src), "\n");
	for(int i = 0, l = bufs.size(); i < l; i++){
		vector<string> bufs_ = slipt(bufs[i], " ");
		if(bufs_[0].compare(0, 1, "#") && bufs_.size() > 1){
			vector<string> bufs__;
			for(int j = 1, l = bufs_.size(); j < l; j++){
				bufs__.push_back(bufs_[j]);
			}
			propertices[bufs_[0]] = bufs__;
		}
	}
	return propertices;
}

vector<string> slipt(string src, string symble){
	vector<string> dest;
	int last = 0, index = 0;
	while((index = src.find_first_of(symble, last)) >= 0){
		if(index - last){
			dest.push_back(src.substr(last, index - last));
		}
		last = index + 1;
	}
	if(src.length() - last){
		dest.push_back(src.substr(last, src.length() - last));
	}
	return dest;
}